<script setup lang="ts" name="SvgIcon">

import {computed, CSSProperties} from "vue";

type SvgIconProps = {
    prefix?: string
    name: string
    color?: string
    size?: string | number
    spin?: boolean
}

const { name ,prefix = 'icon', color='#ffffff',size=20,spin=false } = defineProps<SvgIconProps>()

const symbolId = computed(()=>`#${prefix}-${name}`)
const getStyle = computed(():CSSProperties =>{
    let s = `${size}`;
    s = `${s.replace('px', '')}px`
    return {
        fontSize: s
    }
})

</script>

<template>
  <i :class="['el-icon', spin && 'svg-icon-spin']" :style="getStyle">
      <svg aria-hidden="true">
          <use :xlink:href="symbolId" :fill="color"/>
      </svg>

  </i>
</template>

<style scoped lang="scss">
.el-icon {
  --color: inherit;

  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1em;
  height: 1em;
  font-size: inherit;
  line-height: 1em;
  color: var(--color);
  fill: currentColor;

  svg {
    width: 1em;
    height: 1em;
  }
}

.svg-icon-spin {
  animation: circle 1.5s infinite linear;
}

/* 旋转动画 */
@keyframes circle {
  0% {
    transform: rotate(0);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>